package com.suxii.netty_demo.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smart.refresh.header.BezierRadarHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.suxii.adapter.MessageAdapter;
import com.suxii.netty_demo.R;
import com.suxii.pojo.MessageMsg;
import com.suxii.utils.Global;
import com.suxii.utils.NotificationUtils;

import java.util.ArrayList;
import java.util.List;

public class MessageFragment extends Fragment {

    private Context context;

    public MessageFragment(Context context) {
        this.context = context;
    }

    private RecyclerView messageList;
    public static MessageAdapter messageAdapter=null;

    private TextView nolistText;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_message, container, false);

        messageList = view.findViewById(R.id.message_list);
        List<MessageMsg> list = new ArrayList<>();


        LinearLayoutManager manager = new LinearLayoutManager(context);
        messageList.setLayoutManager(manager);

        messageAdapter = new MessageAdapter(list, context);
        messageAdapter.findMessage();
        messageList.setAdapter(messageAdapter);

        nolistText=view.findViewById(R.id.message_nolist_test);
        nolistText.setVisibility(messageAdapter.getItemCount()==0?View.VISIBLE:View.GONE);

        // TODO 长按弹窗
        // 列表长按事件
        messageAdapter.setItemLongClickListener(v -> {
            showPopupWindow(v);
            return true;
        });

        setLond(view);
        return view;
    }

    private void setLond(View view) {
        RefreshLayout refreshLayout = view.findViewById(R.id.refreshLayout);

        refreshLayout.setRefreshHeader(new BezierRadarHeader(context).setEnableHorizontalDrag(true));
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            messageAdapter.findMessage();
            refreshlayout.finishRefresh(1500);
            nolistText.setVisibility(messageAdapter.getItemCount()==0?View.VISIBLE:View.GONE);
            Global.isConnect();
        });

    }


    private PopupWindow popupWindow;

    // 显示弹窗
    private void showPopupWindow(View anchorView) {
        // 加载弹窗布局
        View popupView = LayoutInflater.from(context).inflate(R.layout.popup_message, null);

        // 创建PopupWindow对象
        popupWindow = new PopupWindow(popupView, anchorView.getWidth()/3, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        // 设置弹窗的一些属性
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);

        popupView.findViewById(R.id.popup_message_text).setOnClickListener(v->{
            NotificationUtils.getInstance(context).sendNotice(66666,"66666","新消息");
            popupWindow.dismiss();
        });

        // 显示弹窗
        popupWindow.showAsDropDown(anchorView,anchorView.getWidth()/3,-anchorView.getHeight()/3);


    }

}
